package com.wpf.system.common.utils;

import com.wpf.system.entity.Field;
import com.wpf.system.entity.vo.CascaderVO;
import com.wpf.system.entity.Department;
import com.wpf.system.entity.Menu;
import com.wpf.system.entity.vo.RouterVO;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 生成菜单树
 */
public class MakeTree {
    public static List<Menu> makeMenuTree(List<Menu> menuList, Long pid){
        List<Menu> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getPid() == pid)
                .forEach(item ->{
                    Menu menu = new Menu();
                    BeanUtils.copyProperties(item,menu);
                    //获取每一个item的下级
                    List<Menu> children = makeMenuTree(menuList, item.getId());
                    menu.setChildren(children);
                    list.add(menu);
                });
        return list;
    }

    public static List<CascaderVO> makeMenuCascader(List<Menu> menuList, Long pid){
        List<CascaderVO> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getPid() == pid)
                .forEach(item -> {
                    CascaderVO cascaderVO = new CascaderVO();
                    cascaderVO.setValue(item.getId());
                    cascaderVO.setLabel(item.getLabel());
                    List<CascaderVO> children = makeMenuCascader(menuList, item.getId());
                    cascaderVO.setChildren(children);
                    list.add(cascaderVO);
                });
        return list;
    }

    public static List<RouterVO> makeRouter(List<Menu> menuList, Long pid){
        List<RouterVO> list = new ArrayList<>();
        Optional.ofNullable(menuList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getPid() == pid)
                .forEach(item -> {
                    RouterVO routerVO = new RouterVO();
                    routerVO.setName(item.getName());
                    routerVO.setPath(item.getPath());
                    //如果路由带含有【 /:】则表示是功能级子路由
                    boolean c = item.getPath().contains("/:");
                    if(c){
                        routerVO.setHidden(true);
                    }else{
                        routerVO.setHidden(false);
                    }
                    if(item.getPid() == 0L){
                        routerVO.setComponent("Layout");
                        //父级组件的重定向路径
                        routerVO.setRedirect("noRedirect");
                        //如果子项大于2，显示根路由
                        if(item.getChildren().size()>1){
                            routerVO.setAlwaysShow(true);
                        }else{
                            routerVO.setAlwaysShow(false);
                        }
                    }else{
                        routerVO.setComponent(item.getComponent());
                        routerVO.setAlwaysShow(false);
                    }
                    //设置meta
                    routerVO.setMeta(routerVO.new Meta(
                            item.getLabel(),
                            item.getIcon(),
                            c,
                            item.getPerms().split(",")
                    ));
                    //设置children,每一项的下级
                    List<RouterVO> children = makeRouter(menuList, item.getId());
                    routerVO.setChildren(children);
                    list.add(routerVO);
                });
        return list;
    }

    public static List<Department> makeDeptTree(List<Department> deptList, Long pid){
        List<Department> list = new ArrayList<>();
        Optional.ofNullable(deptList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getPid() == pid)
                .forEach(item ->{
                    Department department = new Department();
                    BeanUtils.copyProperties(item,department);
                    //获取每一个item的下级
                    List<Department> children = makeDeptTree(deptList, item.getId());
                    department.setChildren(children);
                    list.add(department);
                });
        return list;
    }

    public static List<CascaderVO> makeDeptCascader(List<Department> deptList, Long pid){
        List<CascaderVO> list = new ArrayList<>();
        Optional.ofNullable(deptList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getPid() == pid)
                .forEach(item -> {
                    CascaderVO cascaderVO = new CascaderVO();
                    cascaderVO.setValue(item.getId());
                    cascaderVO.setLabel(item.getDeptName());
                    List<CascaderVO> children = makeDeptCascader(deptList, item.getId());
                    cascaderVO.setChildren(children);
                    list.add(cascaderVO);
                });
        return list;
    }

    public static List<Field> makeFieldTree(List<Field> fieldList, Long pid){
        List<Field> list = new ArrayList<>();
        Optional.ofNullable(fieldList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getPid() == pid)
                .forEach(item ->{
                    Field field = new Field();
                    BeanUtils.copyProperties(item,field);
                    //获取每一个item的下级
                    List<Field> children = makeFieldTree(fieldList, item.getId());
                    field.setChildren(children);
                    list.add(field);
                });
        return list;
    }
}
